package com.linktco.common.utils;

import com.linktco.common.constant.WxTokenConstant;
import com.linktco.common.entity.LinkUser;
import io.jsonwebtoken.Claims;
import org.springblade.core.launch.constant.TokenConstant;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.support.Kv;
import org.springblade.core.tool.utils.Func;
import org.springblade.core.tool.utils.WebUtil;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

import static com.linktco.common.constant.CommonConstant.*;


/**
 * 用户工具类
 *
 * @author zhangnx
 */
public class UserUtil extends  AuthUtil {

    public static LinkUser getUser() {
        HttpServletRequest request = WebUtil.getRequest();
        if (request==null){
            return null;
        }
        Claims claims = getClaims(request);
        if (claims == null) {
            return null;
        } else {

            LinkUser user = new LinkUser();
            user.setClientId(Func.toStr(claims.get(TokenConstant.CLIENT_ID)));
            user.setUserId(Func.toLong(claims.get(TokenConstant.USER_ID)));
            user.setTenantId(Func.toStr(claims.get(TokenConstant.TENANT_ID)));
            user.setOauthId(Func.toStr(claims.get(TokenConstant.OAUTH_ID)));
            user.setAccount(Func.toStr(claims.get(TokenConstant.ACCOUNT)));
            user.setDeptId(Func.toStrWithEmpty(claims.get(TokenConstant.DEPT_ID), "-1"));
            user.setPostId(Func.toStrWithEmpty(claims.get(TokenConstant.POST_ID), "-1"));
            user.setRoleId(Func.toStrWithEmpty(claims.get(TokenConstant.ROLE_ID), "-1"));
            user.setRoleName(Func.toStr(claims.get(TokenConstant.ROLE_NAME)));
            user.setUserName(Func.toStr(claims.get(TokenConstant.USER_NAME)));
            user.setNickName(Func.toStr(claims.get(TokenConstant.NICK_NAME)));
            user.setOpenId(Func.toStr(claims.get(WxTokenConstant.OPEN_ID)));
            user.setPhone(Func.toStr(claims.get(WxTokenConstant.PHONE)));
            user.setBusinessId(Func.toLong(claims.get(WxTokenConstant.BUSINESS_ID)));

            Long organId = claims.get(ORGAN_ID, Long.class);
            user.setOrganId(organId);
            Integer dataScopeId = claims.get(DATA_SCOPE_ID, Integer.class);
            user.setDataScopeId(dataScopeId);
            String queryDeptIds = claims.get(DEPT_DATA_IDS, String.class);
            user.setQueryDeptIds(queryDeptIds);
            String queryBusinessIds = claims.get(BUSINESS_DATA_IDS, String.class);
            user.setQueryBusinessIds(queryBusinessIds);
            Kv detail = Kv.create().setAll((Map) claims.get("detail"));
            user.setDetail(detail);

            return user;
        }


    }

    public static String getOpenId() {
        LinkUser user = getUser();
        return null == user ? null : user.getOpenId();
    }


    public static String getPhone() {
        LinkUser user = getUser();
        return null == user ? null : user.getPhone();
    }

    public static Long getOrganId() {
        LinkUser user = getUser();
        return null == user ? null : user.getOrganId();
    }

    public static Long getUserId() {
        LinkUser user = getUser();
        return null == user ? -1L : user.getUserId();
    }

    public static List<Long> getQueryDeptIds() {
        LinkUser user = getUser();
        if (user == null) {
            return null;
        }
        if (U.isEmpty(user.getQueryDeptIds())) {
            return null;
        }
        return  Func.toLongList(user.getQueryDeptIds());
    }

    public static List<Long> getQueryBusinessIds() {
        LinkUser user = getUser();
        if (user == null) {
            return null;
        }
        if (U.isEmpty(user.getQueryBusinessIds())) {
            return null;
        }
        return  Func.toLongList(user.getQueryBusinessIds());
    }

}
